export class AsyncQueue {
    private tasks: (() => Promise<any>)[] = [];
    private concurrency: number;
    private running = 0;
    private results: any[] = [];
  
    constructor(concurrency: number) {
      this.concurrency = concurrency;
    }
  
    enqueue(task: () => Promise<any>): void {
      this.tasks.push(task);
      this.run();
    }
  
    async awaitAll(): Promise<any[]> {
      while (this.running > 0 || this.tasks.length > 0) {
        await new Promise(resolve => setTimeout(resolve, 10));
      }
      return this.results;
    }
  
    private async run(): Promise<void> {
      while (this.running < this.concurrency && this.tasks.length > 0) {
        const task = this.tasks.shift();
        if (task) {
          this.running++;
          task().then((result) => {
            this.running--;
            this.run();
            this.results.push(result);
          });
        }
      }
    }
  }
  